﻿using System;
using Validity.Contexts;
using Validity.Localization;
using Validity.Rules;

namespace Validity
{
  public static class HasMinimumLengthOfConstraint
  {
    public static IConditionalValidationContext<TObject, String> HasMinimumLengthOf<TObject>(this IValidationContext<TObject, String> context, Int32 length)
    {
      Verify.True(length >= 0);

      context.AddRule(new Rule<TObject, String>(
        (obj, val) => (val ?? String.Empty).Length >= length,
        (obj, val) => String.Format(RuleViolations.MinimumLength, length, (val ?? String.Empty).Length)
      ));

      return new ConditionalValidationContext<TObject, String>(context);
    }
  }
}
